﻿using Lion.WeiXin.Core.Data;
using Lion.WeiXin.Core.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lion.WeiXin.Controllers
{
    [PermissionFilter]
    public class OperateLogController : Controller
    {
         DBContext db;

        public OperateLogController()
            : this(null)
        { 
            
        }
        
        public OperateLogController(DBContext mockDB)
        {
            if (mockDB == null)
            {
                db = new DBContext();
            }
            else {
                db = mockDB;
            }
        }

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///  获取分页列表数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="msgType"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public string GetPageList(string name, int currentPage, int pageSize)
        {
            //如果传入条件参数
            name = HttpUtility.UrlDecode(name);
            List<OperateLogModel> list = new List<OperateLogModel>();
            int totalCount = 0;
            string where = string.Format(" LogType = '{0}'", name);
            if (!string.IsNullOrEmpty(name))
            {
                list = db.Logs.GetList(pageSize, currentPage,where );
                totalCount = db.Logs.GetRecordCount(where);
            }
            else
            {
                list = db.Logs.GetList(pageSize, currentPage, "");
                totalCount = db.Logs.GetRecordCount("");
            }

            string jsonResult = JsonConvert.SerializeObject(list, new IsoDateTimeConverter());
            jsonResult = "{\"total\":" + totalCount + "," + "\"data\":" + jsonResult + "}";
            return jsonResult;
        }

    }
}
